// pages/orders/detail.js
const ins = getApp()

Page({
  data: {
    request: null,
    comment: {
      list: [],
      page: {
        page: 1,
        offset: 10
      },
      hasNextPage: true,
      fetching: false,
      current_text: '',
      requesting: false
    },
    role_type: {
      'SEEKER': '求兼职',
      'EMPLOYER': '招兼职'
    },
    status_text: [
      '待处理',
      '进行中',
      '已结束',
      '已拒绝',
      '已撤销'
    ],
    is_self: true
  },
  onLoad: function (options) {
    this.setData({
      is_self: options.is_self === 'true'
    })

    this.setData({
      request: wx.getStorageSync('request_detail'),
    }, () => {
      wx.removeStorageSync('request_detail')

      if(options.id == undefined || this.data.request == null || this.data.request.id != options.id) {
        wx.navigateBack({
          delta: 0,
          complete: () => {
            wx.showToast({
              title: '数据异常',
              icon: 'error'
            })
          }
        })
      } else {
        this.fetchComment()
      }
    })
  },
  fetchRequest() {
    ins.api.requestApi('request/detail/' + this.data.request.id, 'GET', {}, {
      success: (resp) => {
        if(resp.data.ret == ins.globalData.request_ret.SUCCESS) {
          this.setData({request: resp.data.data})
        }
      }
    })
  },
  fetchComment() {
    if(this.data.comment.fetching || !this.data.comment.hasNextPage) return false

    this.setData({'comment.fetching': true})
    ins.api.requestApi('request/comment/list', 'POST', {
      request_id: this.data.request.id,
      page: this.data.comment.page,
    }, {
      complete: () => {
        this.setData({'comment.fetching': false})
      },
      success: (resp) => {
        const data = resp.data
        if(data.ret == ins.globalData.request_ret.SUCCESS) {
          if(data.data.length < this.data.comment.page.offset) {
            this.setData({'comment.hasNextPage': false})
          }

          if(data.data.length > 0) {
            this.setData({
              ['comment.list[' + this.data.comment.list.length + ']']: data.data,
              'comment.page.page': this.data.comment.page.page + 1
            })
          }
        }
      }
    })
  },
  commentContentChange(e) {
    this.setData({'comment.current_text': e.detail.value})
  },
  toComment() {
    if(this.data.comment.requesting || this.data.comment.current_text.length == 0) return false
    this.setData({'comment.requesting': true})
    wx.showLoading({title: '请稍候', mask: true})

    ins.api.requestApi('request/comment', 'POST', {
      request_id: this.data.request.id,
      content: this.data.comment.current_text
    }, {
      complete: () => {
        wx.hideLoading()
        this.setData({'comment.requesting': false})
      },
      success: (resp) => {
        if(resp.data.ret == ins.globalData.request_ret.SUCCESS) {
          this.setData({
            ['comment.list[' + this.data.comment.list.length + ']']: [
              {
                username : '我',
                content: this.data.comment.current_text,
                date: '刚刚'
              },
            ],
            'comment.current_text': ''
          })
        }

        wx.showToast({
          title: resp.data.data != null ? resp.data.data[0] : resp.data.msg,
          icon: 'none'
        })
      }
    })
  },
  cancelRequest() {
    wx.showLoading({title: '请稍候'})

    ins.api.requestApi('request/cancel', 'POST', {
      id: this.data.request.id,
      pid: this.data.request.pid
    }, {
      complete: () => {
        wx.hideLoading()
      },
      success: (resp) => {
        if(resp.data.ret == ins.globalData.request_ret.SUCCESS) {
          this.fetchRequest()
          wx.showToast({
            title: '撤销成功',
            icon: 'success'
          })
        } else {
          wx.showToast({
            title: resp.data.data != null ? resp.data.data[0] : resp.data.msg,
            icon: 'error'
          })
        }
      },
      fail: () => {
        wx.showToast({
          title: '服务器错误',
          icon: 'error'
        })
      }
    })
  },
  allowRequest() {
    wx.showLoading({title: '请稍候'})

    ins.api.requestApi('request/allow', 'POST', {
      id: this.data.request.id,
      pid: this.data.request.pid
    }, {
      complete: () => {
        wx.hideLoading()
      },
      success: (resp) => {
        if(resp.data.ret == ins.globalData.request_ret.SUCCESS) {
          this.fetchRequest()
          wx.showToast({
            title: '已同意',
            icon: 'success'
          })
        } else {
          wx.showToast({
            title: resp.data.data != null ? resp.data.data[0] : resp.data.msg,
            icon: 'error'
          })
        }
      },
      fail: () => {
        wx.showToast({
          title: '服务器错误',
          icon: 'error'
        })
      }
    })
  },
  rejectRequest() {
    wx.showLoading({title: '请稍候'})

    ins.api.requestApi('request/reject', 'POST', {
      id: this.data.request.id,
      pid: this.data.request.pid
    }, {
      complete: () => {
        wx.hideLoading()
      },
      success: (resp) => {
        if(resp.data.ret == ins.globalData.request_ret.SUCCESS) {
          this.fetchRequest()
          wx.showToast({
            title: '已拒绝',
            icon: 'success'
          })
        } else {
          wx.showToast({
            title: resp.data.data != null ? resp.data.data[0] : resp.data.msg,
            icon: 'error'
          })
        }
      },
      fail: () => {
        wx.showToast({
          title: '服务器错误',
          icon: 'error'
        })
      }
    })
  },
  overRequest() {
    wx.showLoading({title: '请稍候'})

    ins.api.requestApi('request/over', 'POST', {
      id: this.data.request.id,
      pid: this.data.request.pid
    }, {
      complete: () => {
        wx.hideLoading()
      },
      success: (resp) => {
        if(resp.data.ret == ins.globalData.request_ret.SUCCESS) {
          this.fetchRequest()
          wx.showToast({
            title: '订单已结束',
            icon: 'success'
          })
        } else {
          wx.showToast({
            title: resp.data.data != null ? resp.data.data[0] : resp.data.msg,
            icon: 'error'
          })
        }
      },
      fail: () => {
        wx.showToast({
          title: '服务器错误',
          icon: 'error'
        })
      }
    })
  },
  evalRequest() {
    wx.navigateTo({url: '/pages/orders/evaluate?id=' + this.data.request.id + '&pid=' + this.data.request.pid})
  }
})